﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using DataRecv.db;
using DataRecv.models;
using DataRecv.models.road;
using DataRecv.models.device;
using DataRecv.models.recever;
using JRCore.tools;
using JRCore.protocol;
using Microsoft.Win32;
using System.IO;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Collections;
using System.Data;
using DataRecv.tools;

namespace DataRecv
{
    public class G
    {
        /// <summary>
        /// 系统参数
        /// </summary>
        public static SystemParam SystemParam;
        /// <summary>
        /// 路口列表
        /// </summary>
        public static Hashtable Roads;
        /// <summary>
        /// 设备路口视图
        /// </summary>
        public static Hashtable Views;
        /// <summary>
        /// 设备列表
        /// </summary>
        public static List<Device> Devices;
        /// <summary>
        /// 数据处理类
        /// </summary>
        public static Worker Worker;
        /// <summary>
        /// 卡口统计
        /// </summary>
        public static List<KKTongJiParam> KKCount;
        /// <summary>
        /// 电警统计
        /// </summary>
        public static List<DJTongJiParam> DJCount;
        /// <summary>
        /// 磁盘空间清理
        /// </summary>
        public static List<DiskRecyle> DiskRecyles;

        /// <summary>
        /// HK接收
        /// </summary>
        public static Recever_HK Recever_HK;

        /// <summary>
        /// DH接收
        /// </summary>
        public static Recever_DH Recever_DH;

        /// <summary>
        /// 卡口上传客户端
        /// </summary>
        public static Client KKClient;
        /// <summary>
        /// 卡口重传
        /// </summary>
        public static ReUpload KKReUpload;
        /// <summary>
        /// 电警上传客户端
        /// </summary>
        public static Client DJClient;
        /// <summary>
        /// 电警重传
        /// </summary>
        public static ReUpload DJReUpload;
        /// <summary>
        /// BRT车辆列表
        /// </summary>
        public static List<string> BRTs;

        /// <summary>
        /// 内部处理序号
        /// </summary>
        public static int N = 0;

        /// <summary>
        /// 最大并发线程数目
        /// </summary>
        public static int MaxRunningThreads = 0;

        /// <summary>
        /// 第三方推送
        /// </summary>
        public static Server PushServer;

        /// <summary>
        /// 卡口重推
        /// </summary>
        public static RePush KKRePush;

        /// <summary>
        /// 电警重推
        /// </summary>
        public static RePush DJRePush;

        /// <summary>
        /// 设置程序开机自动运行
        /// </summary>
        public static void SetAutoRun()
        {
            RegistryKey reg = null;
            try
            {
                string name = AppDomain.CurrentDomain.FriendlyName;
                string fileName = AppDomain.CurrentDomain.BaseDirectory + name;

                if (File.Exists(fileName))
                {
                    Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                    reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (reg != null)
                    {
                        reg.SetValue(name, fileName);
                    }
                }
            }
            catch
            {

            }
            finally
            {
                if (reg != null)
                {
                    reg.Close();
                }
            }
        }

        /// <summary>
        /// 关闭计算机
        /// </summary>
        public static void ShutDown()
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.WriteLine("shutdown -r -f -t 10");
                p.StandardInput.WriteLine("exit");
            }
            catch { }
        }

        /// <summary>
        /// ping一个ip地址
        /// </summary>
        /// <param name="ip">ip</param>
        /// <returns>通断情况</returns>
        public static bool PingIP(string ip)
        {
            bool ans = false;
            try
            {
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions(128, true);
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.Default.GetBytes(data);
                int timeout = 120;
                PingReply reply = pingSender.Send(System.Net.IPAddress.Parse(ip), timeout, buffer, options);
                if (reply != null)
                {
                    if (reply.Status == IPStatus.Success)
                    {
                        ans = true;
                    }
                }
            }
            catch { }
            return ans;
        }

        /// <summary>
        /// 保存失败记录
        /// </summary>
        /// <param name="record">记录</param>
        /// <returns>结果</returns>
        public static bool SaveFailRecord(DataRecvRecord record)
        {
            string c_TableName = "FailRecord_t";
            string c_Columns = "P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19,P20,P21,P22,P23,P24,P25,P26,P27,P28,P29,P30,P31,P32,P33,P34,P35,P36,P37,P38,P39,P40,P41,P42,P43,P44,P45,P46,P47,P48,P49,P50,P51,P52,P53,uuid";
            bool ans = false;
            try
            {
                if ((record != null) && (record.P != null))
                {
                    string[] vv = new string[DataRecvRecord.N+1];
                    for (int i = 0; i < DataRecvRecord.N; i++)
                    {
                        vv[i] = record.P[i];
                    }
                    vv[DataRecvRecord.N] = Guid.NewGuid().ToString("N");
                    ans = DBHelper.GetInstance().Add(c_TableName, c_Columns, vv);
                }
            }
            catch
            {
                ans = false;
            }
            return ans;
        }

        public static bool SavePushRecord(PicCenterPush record)
        {
            string c_TableName = "PushRecord_t";
            string c_Columns = "P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19,P20,P21,P22,P23,P24,P25,P26,P27,P28,P29,P30,P31,P32,P33,P34,P35,P36,P37,P38,P39,P40,P41,P42,P43,P44,P45,P46,P47,P48,P49,P50,P51,P52,P53,uuid";
            bool ans = false;
            try
            {
                if ((record != null) && (record.P != null))
                {
                    string[] vv = new string[PicCenterPush.N + 1];
                    for (int i = 0; i < PicCenterPush.N+1; i++)
                    {
                        vv[i] = record.P[i];
                    }
                    vv[PicCenterPush.N] = Guid.NewGuid().ToString("N");
                    ans = DBHelper.GetInstance().Add(c_TableName, c_Columns, vv);
                }
            }
            catch
            {
                ans = false;
            }
            return ans;
        }

        public static List<string> LoadBRTs()
        {
            List<string> ans = new List<string>();
            DataTable dt = new DataTable();
            try
            {
                string c_TableName = "BRT_t";
                string c_Columns = "hphm";
                dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "1=1");
                if ((dt != null) && (dt.Rows != null))
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        ans.Add(row["hphm"].ToString());
                    }
                }
            }
            catch { ans = null; }
            finally
            {
                if (dt != null) dt.Dispose();
            }
            return ans;
        }

    }

}
